﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dora.Tools.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using RestSharp;

namespace Dora.ApiGateway.Extension
{
    public class DoraActionFilterAttribute : Attribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var controller = context.Controller ;
            if(controller==null)return;

            if (context.HttpContext.Request.Cookies.TryGetValue("user", out var token))
            {


                var isSuccess = RestHelper.PostJson<bool>("http://localhost:729", 
                    $"/api/oauthhost/oauthhost/validate", new { }, token);
                if (isSuccess.Entity == true) return;
            }

            context.Result = new RedirectResult("/Home/Login");


            // do something before the action executes
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            
            // do something after the action executes
        }
    }

}
